//JS
var app = getApp()
Page({
  data: {
    // 顶部菜单切换
    navbar: ['全部', '代付款', "代发货", "待收货", "已完成"],
    // 默认选中菜单
    currentTab: 0,
    index: 0,
    pick_name: "",
    ordersData: [],
    totalPrice: 0, //合计
    totalSum: 0, //商品数
  },
  // 初始化加载
  onLoad() {
    var that = this;
    this.getValue();
    this.totalPrice();
    this.totalSum();
  },
  onShow() {
    this.getValue();
    this.totalPrice();
    this.totalSum();
  },
  //顶部tab切换
  navbarTap: function (e) {
    this.setData({
      currentTab: e.currentTarget.dataset.idx
    })
  },
  getValue() {
    let back = wx.getStorageSync('dindanData') || [];
    if (Array.isArray(back)) {
      this.setData({
        ordersData: back
      });
    } else {
      this.setData({
        ordersData: []
      });
    }
    this.totalPrice();
  },
  totalPrice() {
    let price = 0;
    this.data.ordersData.forEach((item) => {
      if (item) {
        price += item.price * item.num;
      }
    })
    this.setData({
      totalPrice: price, // 更新页面显示的总价
    });
  },
  totalSum() {
    let sum = 0;
    this.data.ordersData.forEach((item) => {
      if (item) {
        sum += item.num;
      }
    })
    this.setData({
      totalSum: sum,
    });
  }
})